package com.meessiom.train.crm.student.service;

import com.meessiom.train.crm.student.model.Student;
import com.meessiom.train.crm.student.repositroy.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentService {

    @Autowired
    private StudentRepository studentRepository;

    private StudentService studentService;

    //服务为控制器提供获得所有学生列表的方法
    public List<Student> getAllStudent(){
        //调用数据库访问类去访问数据库，从数据库获得所有的学生，
        //并返回给调用者 （studentController
        return studentRepository.findAll();
    }

    public Student createStudent(Student student) {
        return studentRepository.save(student);
    }


    public List<Student> createStudents(List<Student> student) {
        return studentRepository.saveAll(student);
    }



    public Student getStudentById(long id) {
      return  studentRepository.getById(id);
    }

    public void deleteStudent(long id) {

        Student s = studentRepository.getById(id);

        if( null != s )
            studentRepository.delete(s);

    }


}
